////////////////////////////////////////////////////////////////////////////////
// This is the replication file for the graphs in chapters 5 and 6 of
// 
// RULES AND ALLIES: Foreign election interventions
//
// Johannes Bubeck and Nikolay Marinov
//
// 09/04/2019
//
////////////////////////////////////////////////////////////////////////////////

// Set up the following file structure

/*

$home -- (data_consolidated) -- |
  |                            | -- (intermediate_data) -- replication_data_final.dta
  |	  
  | --- (graphs_production_v2)
  
*/

global home "C:\Users\jbube\Documents\Dropbox\Book Bubeck Marinov\" //<<< modify to your home!
global home "C:\Users\Johannes\Dropbox\Book Bubeck Marinov\" //<<< modify to your home!
cd "$home\data_consolidated" 
capture noisily mkdir ..\graphs_production_v2
capture noisily confirm file .\intermediate_data\replication_data_final.dta

use .\intermediate_data\replication_data_final.dta, replace

////////////////////////////////////////////////////////////////////////////////
// Set up graphs
////////////////////////////////////////////////////////////////////////////////

// Edit > Preferences > Graph Preferences "Set Font to Times New Roman"

set scheme s2mono
global text_adjustment_factor = 1.5

grstyle init
grstyle set plain
grstyle gsize heading `=10/$text_adjustment_factor' // title
grstyle gsize subheading `=8/$text_adjustment_factor' // subtitle
grstyle gsize axis_title `=8/$text_adjustment_factor'
grstyle gsize tick_label `=8/$text_adjustment_factor'
grstyle gsize key_label `=8/$text_adjustment_factor' // key labels in legend
grstyle gsize plabel `=6/$text_adjustment_factor' // marker labels
grstyle gsize text_option `=6/$text_adjustment_factor' // added text
grstyle gsize small_body `=6/$text_adjustment_factor' // note size
grstyle linewidth legend none // legend outline

////////////////////////////////////////////////////////////////////////////////
// Generate basic variables for analysis
////////////////////////////////////////////////////////////////////////////////

*include all elections
gen sample=1

*a full measure of polarization
gen pi = a_gov-b_opp if (a_gov!=. & b_opp!=.)
replace pi = a_gov if (a_gov!=.c & b_opp==.c)
replace pi = -b_opp if (a_gov==.c & b_opp!=.c)
*absolute value of polarization
gen abs_pi=abs(pi)

*a trichotomous measure of polarization (harmonized with the pi measure)
gen polarization = .
replace polarization=1 if pi>0 
replace polarization=-1 if pi<0 
replace polarization=0 if pi==0

*a measure of wars (stronger and weaker powers)
gen i_power = ""
replace i_power = "H" if hegwar_li==1 | hegwar_li==2 //hegemon are I and L
replace i_power = "A" if i_apower==1 & !(hegwar_li==1 | hegwar_li==2)
replace i_power = "b" if i_bpower==1 & !(hegwar_li==1 | hegwar_li==2)

*a mesure for democracy in the target country
gen t_lib08 = .
replace t_lib08=1 if (v2x_polyarchy_target>.80 &  v2x_polyarchy_target<=1)
replace t_lib08=0 if (v2x_polyarchy_target<=.80 &  v2x_polyarchy_target>=0)

*a mesure for intervener liberalism

gen i_lib = .
replace i_lib=1 if (v2x_polyarchy_intervener>.60 &  v2x_polyarchy_intervener<=1)
replace i_lib=0 if (v2x_polyarchy_intervener<=.60 &  v2x_polyarchy_intervener>=0)
label define liberallabel 1 "Liberal Power (v2x_polyarchy>.60)" 0 "Illiberal Power (v2x_polyarchy<=60)" 
label values i_lib liberallabel

*number of interveners
bysort electionid: egen interveners_sample = sum(sample)
bysort electionid: egen interveners_total = count(sample)
gen interveners_other = interveners_total-interveners_sample

*tag
egen electionid_uni=tag(electionid) if sample==1

*clearly all of those seem to be international organizations
tab intervener if i_ccode>1000

*Dem IOs
gen demio=(i_ccode>=1000)
gen demiopro=(demio==1 & p>0)  if sample==1 & !missing(p)
gen demiop=(demio==1 & abs(p)>0) if sample==1 & !missing(p)
gen powerp=(demio==0 & abs(p)>0) if sample==1 & !missing(p)

*periods
gen period = .
replace period = 1 if year<=1975
replace period = 2 if year<=1990 & year>1975
replace period = 3 if year<=2001 & year>1990
replace period = 4 if year>2001
label define periodlabel 1 "1945-1975" 2 "1976-1990" 3 "1991-2001" 4 "2002-2012"
label values period periodlabel

*incumbent vote gain
gen gaininc = 0 if nelda27=="yes"
replace gaininc = 1 if nelda27=="no"

*bias favoring incument
gen biasinc = 0 if nelda11=="no"
replace biasinc = 1 if nelda11=="yes"

*Superpowers US UK France Germany Russia India China
gen i_superpower=(i_ccode==2 	|	i_ccode==200	|	i_ccode==365	|	i_ccode==710	|	i_ccode==220	|	i_ccode==260 | i_ccode==750)
replace i_superpower=3 if demio==1	

* Basic stats for each of the main iv, dvs

foreach i of varlist c p polarization demiopro demiop powerp {

	bysort electionid: egen `i'_minimum=min(`i') if sample==1
	bysort electionid: gen `i'_abs=abs(`i'_minimum) if sample==1
	bysort electionid: egen `i'_max=max(`i') if sample==1
	egen `i'dum=rowmax(`i'_abs `i'_max) if sample==1
	replace `i'dum=1 if `i'dum==.5
	gen intervention_`i' = 1 if `i'dum==1 & electionid_uni==1 & interveners_sample>0
	label variable  intervention_`i' "Intervention (`i')"
	gen no_intervention_`i' = 1 if `i'dum==0  & electionid_uni==1 & interveners_sample>0
	label variable  no_intervention_`i' "No Intervention (`i')"
	gen no_coding_`i' = 1 if interveners_sample==0 & interveners_total>0 & `i'dum==1 & electionid_uni==1
	label variable no_coding_`i' "Intervention does not meet sample criteria"
	
	}

////////////////////////////////////////////////////////////////////////////////
// Recoding interventions
////////////////////////////////////////////////////////////////////////////////

gen c_abs_intervener = abs(c)
gen p_abs_intervener = abs(p)

gen c_dum_intervener = 1 if c!=0 & c!=.
replace c_dum_intervener = 0 if c_dum_intervener==.

gen p_dum_intervener = 1 if p!=0 & p!=.
replace p_dum_intervener = 0 if p_dum_intervener==.

gen someinter =  1 if (pdum==1 | cdum==1) & sample==1 & interveners_sample>0

////////////////////////////////////////////////////////////////////////////////
// Recoding and labeling scenarios
////////////////////////////////////////////////////////////////////////////////

gen war_ind=.
replace war_ind = 1 if hegwar_li!=1 & hegwar_li!=2

label variable hegdum "Election Hegemon"
label variable wardum "Election War"

label define iliblabel  0 "Illiberal Intervener" 1 "Liberal Intervener"
label values i_lib iliblabel
	
gen i_power_nice2 = "" //important later on!
replace i_power_nice2 = "Hegemon" if i_power=="H"
replace i_power_nice2 = "War Against Illiberal Power" if hegwar_li==3 | hegwar_li==4

label define LI_long 1 "Liberal Hegemon" 2 "Illiberal Hegemon" 3 "Liberal|(Illiberal)" 4 "Illiberal|(Liberal)" 5 "Liberal|Liberal" 6 "Illiberal|Illiberal" 
label val hegwar_li LI_long
label var hegwar_li "Liberal-Illiberal Hegemony/War"

////////////////////////////////////////////////////////////////////////////////
// Generate useful variables for International Organizations
////////////////////////////////////////////////////////////////////////////////

gen p_pro_IO = "Process Intervention by IO" if demioprodum==1
replace p_pro_IO = "No Intervention by IO" if demioprodum==0

gen p_inter_power = p if sample==1 & demio!=1
gen p_inter_power_abs = p_abs_intervener if sample==1 & demio!=1
bysort electionid: egen demioprodum_intervention = max(demioprodum)
gen p_pro_IO_intervention = "Process Intervention by IO" if demioprodum_intervention==1
replace p_pro_IO_intervention = "No Intervention by IO" if demioprodum_intervention==0

gen p_inter_power_IO = p_inter_power if p_pro_IO_intervention=="Process Intervention by IO"
label variable p_inter_power_IO "Process Intervention by IO"
gen p_inter_power_noIO = p_inter_power if p_pro_IO_intervention=="No Intervention by IO"
label variable p_inter_power_noIO "No Intervention by IO"

cap label define io 0 "No Intervention by IO" 1 "Process Intervention by IO"
label values demioprodum_intervention io

////////////////////////////////////////////////////////////////////////////////
/*
Count of war vs. hegemony
*/
////////////////////////////////////////////////////////////////////////////////

graph bar (sum) hegdum wardum if electionid_uni==1 & sample==1, over(period) stack ///
	ytitle("Number of Elections") legend(lab(2 `"Election War"') lab(1 `"Election Hegemon"') order(1 2) rows(1)) ///
	/*note("Based on variable hegwar from Appendix table")*/ /* bar(1, color(black*0.6)) bar(2, color(black*0.2))*/
graph export  ..\graphs_production_v2\fig_5_1_war_hegemony.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Count of candidate interventions
*/
////////////////////////////////////////////////////////////////////////////////

graph bar (sum) intervention_c no_intervention_c no_coding_c if electionid_uni==1 & sample==1, over(period) stack ///
	legend(lab(1 `"Election with Candidate Intervention(s)"') lab(2 `"Election without Candidate Intervention(s)"') order(1 2) rows(2)) ytitle("Number of Elections")	
graph export  ..\graphs_production_v2\fig_5_2a_c_interventions_by_period.pdf, replace

/*----------------------------------------------------------------------------*/

cap drop c_abs_mean
bysort period sample: egen c_abs_mean = mean(c_abs_intervener) if sample==1

bysort c_abs_intervener period sample: gen howmany=_N if sample==1
egen tag=tag(c_abs_intervener period sample) if sample==1
sort period
twoway (scatter c_abs_intervener period [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
xtitle("Time Period", margin(0 0 0 2)) ytitle("(Absolute) Candidate Intervention", margin(0 2 0 0)) xlabel(,valuelabel)  /*graphregion(margin(large))yline(0, lcolor(red*.25)) */ ) ///
(line c_abs_mean period if sample==1 & tag==1, color(black*.5) lwidth(0.25) lpattern(dash) yaxis(2)) ///
(scatter c_abs_mean period if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.5) mfcolor(white) yaxis(2)) ///
(scatter c_abs_intervener period if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(4) lab(4 "Average (Absolute) Candidate Intervention (Right Scale)" ) span size(large)) ytitle("", axis(2)) ylab(0 (.125) 0.25, axis(2)) ///
graphregion(margin(6 4 2 4))
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_2b_c_intervention_avg_over_time.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Joint use of process and candidate interventions
*/
////////////////////////////////////////////////////////////////////////////////

* Generate a regression table

label variable c_dum_intervener "\$ \mathbf{1}_{(\text{Candidate\_intervention}_{i} \neq 0)} \$"
reg p_dum_intervener c_dum_intervener if sample==1 & (c_dum_intervener==1 | p_dum_intervener==1), noconst vce(robust)
outreg2 using "../tables_production/table_5_1_joint_use.tex" ,tex(fragment) label replace ///
ctitle("\$ \mathbf{1}_{(\text{Process\_intervention}_{i} \neq 0)} \$") ///
addtext(Interventions only, Yes, Full sample, No)

reg p_dum_intervener c_dum_intervener if sample==1, vce(robust)
outreg2 using "../tables_production/table_5_1_joint_use.tex" ,tex(fragment) label append ///
ctitle("\$ \mathbf{1}_{(\text{Process\_intervention}_{i} \neq 0)} \$") ///
addtext(Interventions only, No, Full sample, Yes)

////////////////////////////////////////////////////////////////////////////////
/*
First look at process interventions
*/
////////////////////////////////////////////////////////////////////////////////

graph bar (sum) intervention_p no_intervention_p no_coding_p if electionid_uni==1 & sample==1, over(period) stack ///
	legend(lab(1 `"Election with Process Intervention(s)"') lab(2 `"Election without Process Intervention(s)"') order(1 2) rows(2)) ytitle("Number of Elections")	
graph export  ..\graphs_production_v2\fig_5_3a_p_interventions_by_period.pdf, replace

/*----------------------------------------------------------------------------*/

cap drop p_mean_time
bysort period sample: egen p_mean_time = mean(p) if sample==1


bysort p period sample: gen howmany=_N if sample==1
egen tag=tag(p period sample) if sample==1
sort period
twoway (scatter p period [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
xtitle("Time Period", margin(0 0 0 2)) ytitle("Process Intervention") xlabel(,valuelabel) graphregion(margin(large)) yline(0, lcolor(black*.33)) ) ///
(line p_mean_time period if sample==1 & tag==1, color(black*.5) lwidth(0.25) lpattern(dash)  /*yaxis(2)*/) ///
(scatter p_mean_time period if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.5) mfcolor(white)/* yaxis(2)*/) ///
(scatter p period if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(3) lab(3 "Average Process Intervention")) /*ytitle("", axis(2)) ylab(-.5 (.25) 0.5, axis(2))*/ ///
graphregion(margin(2 10 0 4))
drop howmany tag
graph export ..\graphs_production_v2\fig_5_3b_p_intervention_avg_over_time.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Interventions in emerging vs. consolidated democracies
*/
////////////////////////////////////////////////////////////////////////////////
	
* Interventions in emerging democracies

gen edonlyp0 =  1 if sample==1 & t_lib08==0 & electionid_uni==1 & intervention_p==1 & no_intervention_c==1
gen edonlyc0 =  1 if  sample==1 &  t_lib08==0 & electionid_uni==1 & no_intervention_p==1 & intervention_c==1
gen edboth0  =  1 if sample==1 &  t_lib08==0 & electionid_uni==1 & intervention_p==1 & intervention_c==1
gen edno0    =  1 if sample==1 & t_lib08==0 & electionid_uni==1 & no_intervention_p==1 & no_intervention_c==1

graph bar (sum) edonlyc0 edonlyp0 edboth0 edno0 if sample==1  & electionid_uni==1, over(period) stack bar(1, color(black*0.25)) bar(2, color(black*0.8)) bar(3, color(black*0.4)) bar(4, lcolor(black*0.4) fcolor(none)) ///
legend(span lab(1 "Candidate Intervention") lab(2 "Process Intervention") lab(3 "Process & Candidate Intervention")  lab(4 "No Intervention") rows(2) size(medlarge)) ytitle("Number of Elections", margin(0 2 0 0)) ylab(0 (15) 60) ///
graphregion(margin(4 2 2 2))
graph export  ..\graphs_production_v2\fig_5_5a_interventions_emerging_democracies.pdf, replace

/*----------------------------------------------------------------------------*/

* Interventions in consolidated democracies

gen cdonlyp08 =  1 if sample==1 & t_lib08==1 & electionid_uni==1 & intervention_p==1 & no_intervention_c==1
gen cdonlyc08 =  1 if  sample==1 & t_lib08==1 & electionid_uni==1 & no_intervention_p==1 & intervention_c==1
gen cdboth08  =  1 if sample==1 & t_lib08==1 & electionid_uni==1 & intervention_p==1 & intervention_c==1
gen cdno08    =  1 if sample==1 & t_lib08==1 & electionid_uni==1 & no_intervention_p==1 & no_intervention_c==1

graph bar (sum) cdonlyc08 cdonlyp08 cdboth08 cdno08 if sample==1  & electionid_uni==1, over(period) stack bar(1, color(black*0.25)) bar(2, color(black*0.8)) bar(3, color(black*0.4)) bar(4, lcolor(black*0.4) fcolor(none)) ///
legend(lab(1 "Candidate Intervention") lab(2 "Process Intervention") lab(3 "Process & Candidate Intervention")  lab(4 "No Intervention") rows(2)  size(medlarge) span) ytitle("Number of Elections", margin(0 2 0 0)) ylab(0 (5) 20) ///
graphregion(margin(4 2 2 2))
graph export  ..\graphs_production_v2\fig_5_5b_interventions_consolidated_democracies.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Evolution of polarization over time
*/
////////////////////////////////////////////////////////////////////////////////


graph bar (sum) intervention_polarization no_intervention_polarization no_coding_polarization if electionid_uni==1 & sample==1, over(period) stack ///
	legend(lab(1 `"Election Polarized"') lab(2 `"Election Not Polarized"') order(1 2) rows(2)) ytitle("Number of Elections")	
graph export  ..\graphs_production_v2\fig_5_6a_polarization_by_period.pdf, replace

/*----------------------------------------------------------------------------*/

cap drop abs_pi_mean
bysort period sample: egen abs_pi_mean = mean(abs_pi) if sample==1

bysort abs_pi period sample: gen howmany=_N if sample==1
egen tag=tag(abs_pi period sample) if sample==1
sort period
twoway (scatter abs_pi period [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
xtitle("Time Period", margin(0 0 0 2)) ytitle("Absolute Value of Polarization") xlabel(,valuelabel) graphregion(margin(large)) /*yline(0, lcolor(red*.25)) */ ) ///
(line abs_pi_mean period if sample==1 & tag==1, color(black*.5) lwidth(0.25) lpattern(dash)) ///
(scatter abs_pi_mean period if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.5) mfcolor(white)) ///
(scatter abs_pi period if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(3) lab(3 "Average Absolute Value of Polarization")) ///
graphregion(margin(2 10 0 4))
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_6b_polarization_intervener_avg_over_time.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Relationship between polarization and interventions
*/
////////////////////////////////////////////////////////////////////////////////

reg c pi if sample==1
test pi
local cnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)'"
di "`cnote'"

cap drop c_mean	
bysort pi: egen c_mean = mean(c) if sample==1
twoway  bar c_mean pi if sample==1 , barwidth(.25) lcolor(black) fcolor(none) || lfit c pi if sample==1, yline(0) lcolor(black) lpattern(dash)  /*ciplot(rline)*/ note(`cnote') ///
	legend( lab(1 "Conditional Mean") lab(2 "Linear Model")) xtitle("Polarization") ylab(-1 (.5) 1) ytitle("Candidate Intervention")
graph export  ..\graphs_production_v2\fig_5_7a_c_pi_linearmodel.pdf, replace

/*----------------------------------------------------------------------------*/

bysort c pi sample: gen howmany=_N if sample==1
egen tag=tag(c pi sample) if sample==1
sort pi
twoway (scatter c pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.33))) ///
(bar c_mean pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.33) fcolor(none))  ///
(line c_mean pi if sample==1 & tag==1, color(black*.33) xtitle("Polarization") lwidth(0.25) lpattern(dash)) ///
(scatter c_mean pi if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter c pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(off) ytitle("Candidate intervention")
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_7b_c_pi_visualization.pdf, replace

/*----------------------------------------------------------------------------*/

reg p pi if sample==1
test pi
local pnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)'"
di "`pnote'"

cap drop p_mean
bysort pi: egen p_mean = mean(p) if sample==1
twoway  bar p_mean pi if sample==1 , barwidth(.25) lcolor(black) fcolor(none) || lfit p pi if sample==1, yline(0) lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`pnote') ///
	legend( lab(1 "Conditional Mean") lab(2 "Linear Model")) xtitle("Polarization") ylab(-.5 (.5) .5) ytitle("Process Intervention p")
graph export  ..\graphs_production_v2\fig_5_8a_p_pi_linearmodel.pdf, replace

/*----------------------------------------------------------------------------*/

bysort p pi sample: gen howmany=_N if sample==1
egen tag=tag(p pi sample)if sample==1
sort pi
twoway (scatter p pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.33))) ///
(bar p_mean pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.33) fcolor(none))  ///
(line p_mean pi if sample==1 & tag==1, color(black*.5) xtitle("Polarization") lwidth(0.25) lpattern(dash)) ///
(scatter p_mean pi if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(off) ytitle("Process Intervention p")
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_8b_p_pi_visualization.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Process interventions and intervener liberalism
*/
////////////////////////////////////////////////////////////////////////////////

bysort i_lib: egen p_mean_i_lib = mean(p) if sample==1

bysort p i_lib sample: gen howmany=_N if sample==1
egen tag=tag(p i_lib sample) if sample==1
twoway	(bar p_mean_i_lib i_lib if sample==1 & tag==1, barwidth(.6) lcolor(black) fcolor(none) xlab(0 (1) 1,notick valuelabel)) /// 	
		(scatter p i_lib [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.25))) ///
		(scatter p i_lib if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)), ///
		legend(off) xtitle("") ytitle("Process Intervention")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_5_9_p_liberalism.pdf, replace

/*----------------------------------------------------------------------------*/

reg p pi if sample==1 & i_lib==1
test pi
local pnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
test _cons
local pnote2 "Constant: `: di %5.2f _b[_cons]' (p-value: `: di %5.3f r(p)')"
di "`cnote'"

cap drop p_mean_lib
bysort pi: egen p_mean_lib = mean(p) if sample==1 & i_lib==1
twoway  bar p_mean_lib pi if sample==1 & i_lib==1, barwidth(.25) lcolor(black) fcolor(none) || lfit p pi if sample==1 & i_lib==1, yline(0) lcolor(black) lpattern(dash) /*ciplot(rline)*/ note("`pnote'" "`pnote2'") ///
	legend( lab(1 "Conditional Mean of p") lab(2 "Linear Model")) xtitle("Polarization") ylab(-.75 (.25) .75) ytitle("Process Intervention")
graph export  ..\graphs_production_v2\fig_5_10a_p_pi_linearmodel_liberal.pdf, replace

/*----------------------------------------------------------------------------*/

reg p pi if sample==1 & i_lib==0
test pi
local pnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
test _cons
local pnote2 "Constant: `: di %5.2f _b[_cons]' (p-value: `: di %5.3f r(p)')"
di "`cnote'"

bysort pi: egen p_mean_illib = mean(p) if sample==1 & i_lib==0
twoway  bar p_mean_illib pi if sample==1 & i_lib==0, barwidth(.25) lcolor(black) fcolor(none) || lfit p pi if sample==1 & i_lib==0, yline(0) lcolor(black) lpattern(dash) /*ciplot(rline)*/ note("`pnote'" "`pnote2'") ///
	legend( lab(1 "Conditional Mean of p") lab(2 "Linear Model")) xtitle("Polarization") ylab(-.75 (.25) .75) ytitle("Process Intervention")
graph export  ..\graphs_production_v2\fig_5_10b_p_pi_linearmodel_illiberal.pdf, replace

/*----------------------------------------------------------------------------*/

bysort p pi i_lib sample: gen howmany=_N if sample==1 & i_lib==1
egen tag=tag(p pi i_lib sample) if sample==1 & i_lib==1
sort pi
twoway (scatter p pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.25))) ///
(bar p_mean_lib pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.25) fcolor(none))  ///
(line p_mean_lib pi if sample==1 & tag==1, color(black*.33) xtitle("Polarization") lwidth(0.25) lpattern(dash)) ///
(scatter p_mean_lib pi if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(off) ytitle("Process Intervention")
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_10c_p_pi_visualization_liberal.pdf, replace

/*----------------------------------------------------------------------------*/

bysort p pi i_lib sample: gen howmany=_N if sample==1 & i_lib==0
egen tag=tag(p pi i_lib sample) if sample==1 & i_lib==0
sort pi
twoway (scatter p pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.33))) ///
(bar p_mean_illib pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.33) fcolor(none))  ///
(line p_mean_illib pi if sample==1 & tag==1, color(black*.33) xtitle("Polarization") lwidth(0.25) lpattern(dash)) ///
(scatter p_mean_illib pi if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(off) ytitle("Process Intervention")
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_10d_p_pi_visualization_illiberal.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Interventions and outcomes in the hegemon scenario
*/
////////////////////////////////////////////////////////////////////////////////

cap drop c_mean_heg
bysort pi i_power: egen c_mean_heg = mean(c) if sample==1 & i_power=="H"

reg c pi if sample==1 & i_power=="H"
test pi
local cnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "

bysort c pi i_power sample: gen howmany=_N if sample==1 & i_power=="H"
egen tag=tag(c pi i_power sample) if sample==1 & i_power=="H"
sort pi
twoway (scatter c pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.33))) ///
(scatter c_mean_heg pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter c pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit c pi if sample==1 & i_power=="H", lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`cnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Average Intervention") lab(4 "Linear Model") span cols(3)) ytitle("Candidate Intervention")  xtitle(Polarization)
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_11a_c_pi_linviz_hegemony.pdf, replace

/*----------------------------------------------------------------------------*/

cap drop p_mean_heg
bysort pi i_power: egen p_mean_heg = mean(p) if sample==1 & i_power=="H"

reg p pi if sample==1 & i_power=="H"
test pi
local pnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "

bysort p pi i_power sample: gen howmany=_N if sample==1 & i_power=="H"
egen tag=tag(p pi i_power sample) if sample==1 & i_power=="H"
sort pi
twoway (scatter p pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(black*.33))) ///
(scatter p_mean_heg pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit p pi if sample==1 & i_power=="H", lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`pnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Average Intervention") lab(4 "Linear Model") span cols(3)) ytitle("Process Intervention")  xtitle(Polarization)
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_11b_p_pi_linviz_hegemony.pdf, replace

/*----------------------------------------------------------------------------*/

bysort pi i_power: egen gaininc_mean = mean(gaininc) if sample==1 & i_power=="H"

reg gaininc pi if sample==1 & i_power=="H"
test pi
local gainnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "

bysort gaininc pi i_power sample: gen howmany=_N if sample==1 & i_power=="H"
egen tag=tag(gaininc pi i_power sample) if sample==1 & i_power=="H"
sort pi
twoway (scatter gaininc pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)) ///
(scatter gaininc_mean pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter gaininc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit gaininc pi if sample==1 & i_power=="H", lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`gainnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Avg. of Gain (0/1)") lab(4 "Linear Model") span cols(3)) ytitle("Incumbent Vote Gain")  ylab(-.25 (.25) 1.25) 
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_11c_gaininc_pi_linviz_hegemony.pdf, replace

/*----------------------------------------------------------------------------*/

bysort pi i_power: egen biasinc_mean = mean(biasinc) if sample==1 & i_power=="H"

reg biasinc pi if sample==1 & i_power=="H"
test pi
local biasincnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
di "`biasincnote'"

bysort biasinc pi i_power sample: gen howmany=_N if sample==1 & i_power=="H"
egen tag=tag(biasinc pi i_power sample) if sample==1 & i_power=="H"
sort pi
twoway (scatter biasinc pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)) ///
(scatter biasinc_mean pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter biasinc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit biasinc pi if sample==1 & i_power=="H", lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`biasincnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Avg. of Bias (0/1)") lab(4 "Linear Model") span cols(3)) ytitle("Bias Favoring Incumbent")  ylab(-.25 (.25) 1.25) xtitle(Polarization)
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_11d_biasinc_pi_linviz_hegemony.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Superpower graphs ----> simplify the absoulte values code
*/
////////////////////////////////////////////////////////////////////////////////

* Preliminaries

cap drop i_superpower_plot
gen i_superpower_plot = i_superpower
replace i_superpower_plot=2 if i_superpower==3

cap label drop superpower_label
label define superpower_label 0 "Normal Power" 1 "Superpower" 2 "Internat. Org."
label values  i_superpower_plot superpower_label

/*----------------------------------------------------------------------------*/

cap drop abs_c_mean_superpower
bysort i_superpower_plot sample: egen abs_c_mean_superpower = mean(c_abs_intervener) if sample==1

bysort c_abs_intervener i_superpower_plot sample: gen howmany=_N if sample==1
egen tag=tag(c_abs_intervener i_superpower_plot sample) if sample==1
sort period
twoway (scatter c_abs_intervener i_superpower_plot [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
ytitle("(Absolute) Candidate Intervention") xlabel(,valuelabel) yline(0, lcolor(black*.33)) ) ///
(bar abs_c_mean_superpower i_superpower_plot if sample==1 & tag==1, color(black*.5) barwidth(0.5) fcolor(none)) ///
(scatter c_abs_intervener i_superpower_plot if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(2) lab(2 "Average (Absolute) Candidate Intervention")) xlab(0 (1) 2) xtitle("") ylab(0 (.25) 1)  ///
graphregion(margin(2 2 2 2))
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_4a_c_intervention_avg_by_powertype.pdf, replace

/*----------------------------------------------------------------------------*/

cap drop p_mean_superpower
bysort i_superpower_plot sample: egen p_mean_superpower = mean(p) if sample==1

bysort p i_superpower_plot sample: gen howmany=_N if sample==1
egen tag=tag(p i_superpower_plot sample) if sample==1
sort period
twoway (scatter p i_superpower_plot [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
ytitle("Process Intervention") xlabel(,valuelabel) yline(0, lcolor(black*.33)) ) ///
(bar p_mean_superpower i_superpower_plot if sample==1 & tag==1, color(black*.5) barwidth(0.5) fcolor(none)) ///
(scatter p i_superpower_plot if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(2) lab(2 "Average Process Intervention")) xlab(0 (1) 2) xtitle("") ///
graphregion(margin(2 2 2 2))
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_4b_p_intervention_avg_by_powertype.pdf, replace


////////////////////////////////////////////////////////////////////////////////
/*
Interventions and outcomes in war scenarios: The role of polarization
*/
////////////////////////////////////////////////////////////////////////////////

cap bysort pi: egen c_mean_war = mean(c) if sample==1 & hegwar_li!=1 & hegwar_li!=2

reg c pi if sample==1 & hegwar_li!=1 & hegwar_li!=2
test pi
local cnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
di "`cnote'"

bysort c pi war_ind sample: gen howmany=_N if sample==1 & war_ind==1
egen tag=tag(c pi war_ind sample) if sample==1 & war_ind==1
sort pi
twoway (scatter c pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.33))) ///
(scatter c_mean_war pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter c pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit c pi if sample==1 & war_ind==1, lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`cnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Average Intervention") lab(4 "Linear Model") span cols(3)) ytitle("Candidate Intervention")  xtitle(Polarization) ///
graphregion(margin(2 2 2 6))
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_12a_c_pi_linviz_war.pdf, replace

/*----------------------------------------------------------------------------*/

cap bysort pi: egen p_mean_war = mean(p) if sample==1 & hegwar_li!=1 & hegwar_li!=2

reg p pi if sample==1 & hegwar_li!=1 & hegwar_li!=2
test pi
local pnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
di "`pnote'"

bysort p pi war_ind sample: gen howmany=_N if sample==1 & war_ind==1
egen tag=tag(p pi war_ind sample) if sample==1 & war_ind==1
sort pi
twoway (scatter p pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.33))) ///
(scatter p_mean_war pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit p pi if sample==1 & war_ind==1, lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`pnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Average Intervention") lab(4 "Linear Model") span cols(3)) ytitle("Process Intervention")  xtitle(Polarization) ///
graphregion(margin(2 2 2 6))
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_12b_p_pi_linviz_war.pdf, replace

/*----------------------------------------------------------------------------*/

reg gaininc pi if sample==1 & hegwar_li!=1 & hegwar_li!=2
test pi
local gainnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
di "`gainnote'"

cap bysort pi: egen gaininc_mean_war = mean(gaininc) if sample==1 & hegwar_li!=1 & hegwar_li!=2

bysort gaininc pi war_ind sample: gen howmany=_N if sample==1 & war_ind==1
egen tag=tag(gaininc pi war_ind sample) if sample==1 & war_ind==1
sort pi
twoway (scatter gaininc pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) ) ///
(scatter gaininc_mean_war pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter gaininc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit gaininc pi if sample==1 & war_ind==1, lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`gainnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Avg. Incumbent Gain") lab(4 "Linear Model") span cols(3)) ytitle("Incumbent Vote Gain")  ylab(-.25 (.25) 1.25)  ///
graphregion(margin(2 2 2 4))
drop howmany tag
graph export  ..\graphs_production_v2\fig_5_12c_gaininc_pi_linviz_war.pdf, replace


/*----------------------------------------------------------------------------*/

reg biasinc pi if sample==1 & hegwar_li!=1 & hegwar_li!=2
test pi
local biasincnote "Slope of the linear model: `: di %5.2f _b[pi]' (p-value: `: di %5.3f r(p)'); R{superscript:2} = `: di %5.2f e(r2)' "
di "`biasincnote'"

cap bysort pi: egen biasinc_mean_war = mean(biasinc) if sample==1 & hegwar_li!=1 & hegwar_li!=2

bysort biasinc pi war_ind sample: gen howmany=_N if sample==1 & war_ind==1
egen tag=tag(biasinc pi war_ind sample) if sample==1 & war_ind==1
sort pi
twoway (scatter biasinc pi [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) ) ///
(scatter biasinc_mean_war pi if sample==1 & tag==1,  msymbol(X) msize(large) mlcolor(black*.33) mfcolor(white) xtitle("Polarization")) ///
(scatter biasinc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
|| lfit biasinc pi if sample==1 & war_ind==1, lcolor(black) lpattern(dash) /*ciplot(rline)*/ note(`biasincnote') ///
, legend(order(1 2 4) lab(1 "Raw Data") lab(2 "Avg. of Bias (0/1)") lab(4 "Linear Model") span cols(3)) ytitle("Bias Favoring Incumbent") ylab(-.25 (.25) 1.25) ///
graphregion(margin(2 2 2 4))
drop howmany tag 
graph export  ..\graphs_production_v2\fig_5_12d_biasinc_pi_linviz_war.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Bias in hegemony and war scenario
*/
////////////////////////////////////////////////////////////////////////////////

cap bysort hegwar_li sample: egen biasinc_mean_hegwar_li = mean(biasinc) if sample==1 & i_power=="H" & electionid_uni==1

bysort biasinc hegwar_li electionid_uni sample: gen howmany=_N if sample==1 & electionid_uni==1
egen tag=tag(biasinc hegwar_li sample electionid_uni) if sample==1 & i_power=="H" & electionid_uni==1
twoway	(bar biasinc_mean_hegwar_li hegwar_li if sample==1 & tag==1, barwidth(.6) lcolor(black) fcolor(none) xlab(1 (1) 2,notick valuelabel)) /// 	
		(scatter biasinc hegwar_li [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.25))) ///
		(scatter biasinc hegwar_li if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)), ///
		legend(off) xtitle("") ytitle("Fraction of Elections with Bias") ///
		graphregion(margin(2 2 2 6))
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_5_13a_biasinc_liberalism_hegemon.pdf, replace

/*----------------------------------------------------------------------------*/

cap bysort hegwar_li sample: egen biasinc_mean_wartype = mean(biasinc) if sample==1 & hegwar_li!=1 & hegwar_li!=2 & electionid_uni==1

bysort biasinc hegwar_li electionid_uni sample: gen howmany=_N if sample==1 & electionid_uni==1
egen tag=tag(biasinc hegwar_li sample electionid_uni) if sample==1 & hegwar_li!=1 & hegwar_li!=2 & electionid_uni==1
twoway	(bar biasinc_mean_wartype hegwar_li if sample==1 & tag==1, barwidth(.75) lcolor(black) fcolor(none) xlab(3 (1) 6,notick valuelabel labsize(medlarge))) /// 	
		(scatter biasinc hegwar_li [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.25))) ///
		(scatter biasinc hegwar_li if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)), ///
		legend(off) xtitle("") ytitle("Fraction of Elections with Bias")  ///
		graphregion(margin(2 2 2 6))
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_5_13b_biasinc_liberalism_war.pdf, replace



////////////////////////////////////////////////////////////////////////////////
/*
Process interventions along with international organizations
*/
////////////////////////////////////////////////////////////////////////////////

graph bar (sum) intervention_demiop no_intervention_demiop no_coding_demiop if electionid_uni==1 & sample==1, over(period) stack ///
	legend(lab(1 `"Process Intervention by International Organization"') lab(2 `"No Process Intervention by Internat. Organization"') order(1 2) rows(2) span) ytitle("Number of Elections")	
graph export  ..\graphs_production_v2\fig_5_14a_p_interventions_IO_by_period.pdf, replace

/*----------------------------------------------------------------------------*/

gen poweronlyp =  1 if demiopdum==0 & powerpdum==1 &  electionid_uni==1 & interveners_sample>0
gen demioonlyp = 1 if demiopdum==1 & powerpdum==0 & electionid_uni==1 & interveners_sample>0
gen bothp =  1 if demiopdum==1 & powerpdum==1 & electionid_uni==1 & interveners_sample>0
gen nop =  1 if demiopdum==0 & powerpdum==0 &  electionid_uni==1 & interveners_sample>0
	
graph bar (sum) poweronlyp bothp demioonlyp /*nop*/ if electionid_uni==1 & sample==1, over(period) stack /*bar(1, color(black*0.1)) bar(2, color(black*0.6)) bar(3, color(black*0.25)) bar(4, color(black*0.8))*/ ///
legend(lab(1 "International Power(s) Only") lab(2 "Joint Interventions") lab(3 "International Organization(s) Only")  /*lab(4 "No process intervention")*/ rows(2) span) ytitle("Number of Elections", margin(0 2 0 0)) ylab(0 (20) 60)
graph export  ..\graphs_production_v2\fig_5_14b_p_interventions_IO_scenario.pdf, replace

/*----------------------------------------------------------------------------*/

bysort demioprodum_intervention sample demio: egen p_mean_IO = mean(p_inter_power) if sample==1 & demio!=1

bysort p demioprodum_intervention sample demio: gen howmany=_N if sample==1 & demio!=1
egen tag=tag(p demioprodum_intervention sample demio) if sample==1 & demio!=1
twoway	(bar p_mean_IO demioprodum_intervention if sample==1 & tag==1, barwidth(.6) lcolor(black) fcolor(none) xlab(0 (1) 1,notick valuelabel)) /// 	
		(scatter p demioprodum_intervention [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.25))) ///
		(scatter p demioprodum_intervention if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)), ///
		legend(off) xtitle("") ytitle("Average Expenditure on Process") ///
		graphregion(margin(4 8 4 4))
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_5_15a_p_interventions_powers_IO.pdf, replace

/*----------------------------------------------------------------------------*/

bysort demioprodum_intervention sample demio: egen p_mean_IO_abs = mean(p_inter_power_abs) if sample==1 & demio!=1

bysort p_inter_power_abs demioprodum_intervention sample demio: gen howmany=_N if sample==1 & demio!=1
egen tag=tag(p_inter_power_abs demioprodum_intervention sample demio) if sample==1 & demio!=1
twoway	(bar p_mean_IO_abs demioprodum_intervention if sample==1 & tag==1, barwidth(.6) lcolor(black) fcolor(none) xlab(0 (1) 1,notick valuelabel)) /// 	
		(scatter p_inter_power_abs demioprodum_intervention [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large)  yline(0, lcolor(black*.25))) ///
		(scatter p_inter_power_abs demioprodum_intervention if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)), ///
		legend(off) xtitle("") ytitle("Average Absolute Expenditure on Process") ///
		graphregion(margin(4 8 4 4))
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_5_15b_p_abs_interventions_powers_IO.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Candidate and process interventions by the U.S.
*/
////////////////////////////////////////////////////////////////////////////////

cap drop p_mean_time_usa
bysort period i_ccode sample: egen p_mean_time_usa = mean(p) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort p i_ccode period sample: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(p period sample intervener) if sample==1 & (intervener=="United States" | i_ccode==2)
sort period
twoway (scatter p period [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
xtitle("Time Period") ytitle("Process Intervention") xlabel(,valuelabel) yline(0, lcolor(black*.33)) ) ///
(line p_mean_time_usa period if sample==1 & tag==1, color(black*.5) lwidth(0.25) lpattern(dash)) ///
(scatter p_mean_time_usa period if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.5) mfcolor(white)) ///
(scatter p period if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(3) lab(3 "Average Process Intervention")) ///
graphregion(margin(2 10 2 6))
drop howmany tag
graph export  ..\graphs_production_v2\fig_6_1a_p_interventions_by_period_usa.pdf, replace

graph bar p if sample==1 & (intervener=="United States" | i_ccode==2), over(pi)  by(period, note("")) ylab(-1 (.5) 1) b2title("Polarization") ytitle("Conditional Mean of Process Intervention") bar(1, fcolor(white) lcolor(black)) yline(0, lcolor(black*.33))
graph export  ..\graphs_production_v2\fig_6_1b_p_pi_by_period_usa.pdf, replace

/*----------------------------------------------------------------------------*/

cap drop c_abs_mean_time_usa
bysort period i_ccode sample: egen c_abs_mean_time_usa = mean(c_abs_intervener) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort c_abs_intervener i_ccode period sample: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(c_abs_intervener period sample intervener) if sample==1 & (intervener=="United States" | i_ccode==2)
sort period
twoway (scatter c_abs_intervener period [w=howmany] if sample==1 & tag==1, msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(medium) ///
xtitle("Time Period") ytitle("(Absolute) Candidate Intervention") xlabel(,valuelabel) graphregion(margin(large)) yline(0, lcolor(black*.33)) ) ///
(line c_abs_mean_time_usa period if sample==1 & tag==1, color(black*.5) lwidth(0.25) lpattern(dash)) ///
(scatter c_abs_mean_time_usa period if sample==1 & tag==1,  msymbol(diamond) msize(large) mlcolor(black*.5) mfcolor(white)) ///
(scatter c_abs_intervener period if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
, legend(order(3) lab(3 "Average (Absolute) Candidate Intervention")) ///
graphregion(margin(2 10 2 6))
drop howmany tag
graph export  ..\graphs_production_v2\fig_6_2a_c_interventions_by_period_usa.pdf, replace


graph bar c if sample==1 & (intervener=="United States" | i_ccode==2), over(pi)  by(period, note("")) ylab(-1 (.5) 1) b2title("Polarization") ytitle("Conditional Mean of Candidate Intervention") bar(1, fcolor(white) lcolor(black)) yline(0, lcolor(black*.33))
graph export  ..\graphs_production_v2\fig_6_2b_c_pi_by_period_usa.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Figures on US interventions: Hegemon vs. war
*/
////////////////////////////////////////////////////////////////////////////////

bysort pi i_power_nice2: egen c_mean_US_ipower=mean(c) if sample==1 & (intervener=="United States" | i_ccode==2)
bysort i_power_nice2: egen c_mean_US_ipower_overall=mean(c) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort c pi i_power_nice2 i_ccode sample: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(c pi i_power_nice2)if sample==1 & (intervener=="United States" | i_ccode==2)
sort pi
ttest c if sample==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2) unequal
return list
local tnote =  "`: di %4.3f `r(mu_1)'' - `: di %4.3f `r(mu_2)'' is p=`: di %4.3f `r(p)'' "
twoway ///  
(bar c_mean_US_ipower pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.66) fcolor(none))  ///
(scatter c pi [w=howmany] if sample==1 & tag==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2,  note("The p-value of a t-test of the difference of means `tnote'") legend(off)) msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(black*.33))) ///
(scatter c pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
(line c_mean_US_ipower_overall pi if sample==1 & tag==1, lcolor(black)  lpattern(solid)) ///
, legend(off) ytitle("Candidate Intervention")  ylab(-1 (.5) 1) xtitle("Polarization")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_6_3a_c_pi_scenario_usa.pdf, replace



/*----------------------------------------------------------------------------*/

bysort pi i_power_nice2: egen p_mean_US_ipower=mean(p) if sample==1 & (intervener=="United States" | i_ccode==2)
bysort i_power_nice2: egen p_mean_US_ipower_overall=mean(p) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort p pi sample i_power_nice2 i_ccode: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(p pi i_power_nice2 sample)if sample==1 & (intervener=="United States" | i_ccode==2)
sort pi
ttest p if sample==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2) unequal
local tnote = "The p-value of a t-test of the difference of means `: di %4.3f `r(mu_1)'' - `: di %4.3f `r(mu_2)'' is p=`: di %4.3f `r(p)'' "
twoway ///  
(bar p_mean_US_ipower pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.5) fcolor(none))  ///
(scatter p pi [w=howmany] if sample==1 & tag==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2, note("`tnote'") legend(off)) msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(red*.25))) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
(line p_mean_US_ipower_overall pi if sample==1 & tag==1, lcolor(black)  lpattern(solid)) ///
, legend(off) ytitle("Process Intervention")  ylab(-1 (.5) 1) xtitle("Polarization")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_6_3b_p_pi_scenario_usa.pdf, replace

/*----------------------------------------------------------------------------*/

bysort pi i_power_nice2: egen gaininc_mean_US_ipower=mean(gaininc) if sample==1 & (intervener=="United States" | i_ccode==2)
bysort i_power_nice2: egen gaininc_mean_US_ipower_overall=mean(gaininc) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort gaininc pi i_power_nice2 i_ccode sample: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(gaininc pi i_power_nice2)if sample==1 & (intervener=="United States" | i_ccode==2)
sort pi
ttest gaininc if sample==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2) unequal
return list
local tnote = "The p-value of a t-test of the difference of means `: di %4.3f `r(mu_1)'' - `: di %4.3f `r(mu_2)'' is p=`: di %4.3f `r(p)'' "
di "`tnote'"
twoway ///  
(bar gaininc_mean_US_ipower pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.66) fcolor(none))  ///
(scatter gaininc pi [w=howmany] if sample==1 & tag==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2,  note("`tnote'") legend(off)) msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(black*.33))) ///
(scatter gaininc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
(line gaininc_mean_US_ipower_overall pi if sample==1 & tag==1, lcolor(black) lpattern(solid)) ///
, legend(off) ytitle("Incumbent Vote Gain")  ylab(-.25 (.25) 1.25) xtitle("Polarization")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_6_3c_gaininc_pi_scenario_usa.pdf, replace

/*----------------------------------------------------------------------------*/

bysort pi i_power_nice2: egen biasinc_mean_US_ipower=mean(biasinc) if sample==1 & (intervener=="United States" | i_ccode==2)
bysort i_power_nice2: egen biasinc_mean_US_ipower_overall=mean(biasinc) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort biasinc pi i_power_nice2 i_ccode sample: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(biasinc pi i_power_nice2)if sample==1 & (intervener=="United States" | i_ccode==2)
sort pi
ttest biasinc if sample==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2) unequal 
local tnote = "The p-value of a t-test of the difference of means `: di %4.3f `r(mu_1)'' - `: di %4.3f `r(mu_2)'' is p=`: di %4.3f `r(p)'' "
twoway ///  
(bar biasinc_mean_US_ipower pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.66) fcolor(none))  ///
(scatter biasinc pi [w=howmany] if sample==1 & tag==1 & (intervener=="United States" | i_ccode==2), by(i_power_nice2,  note("`tnote'") legend(off)) msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(black*.33))) ///
(scatter biasinc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
(line biasinc_mean_US_ipower_overall pi if sample==1 & tag==1, lcolor(black)  lpattern(solid)) ///
, legend(off) ytitle("Bias Favoring Incumbent")  ylab(-.25 (.25) 1.25) xtitle("Polarization")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_6_3d_biasinc_pi_scenario_usa.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
US interventions
*/
////////////////////////////////////////////////////////////////////////////////

gen usinter_helper =1 if sample==1 & someinter == 1 & ((intervener=="United States" | i_ccode==2) | i_ccode==2) & ((p!=. & p!=0) | (c!=. & c!=0))
bysort electionid: egen usinter =  max(usinter_helper) if sample==1
replace usinter = 0 if usinter!=1 & someinter == 1

graph bar usinter if electionid_uni==1 & sample==1, over(period) ytitle("Fraction of Interventions with US Present") //name(g1, replace)
graph export  ..\graphs_production_v2\fig_6_4_presence_us.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Regression to check for time trend vs. presence of IO as driver of US process
interventions
*/
////////////////////////////////////////////////////////////////////////////////

gen p_pro_IO_int = 1 if p_pro_IO_intervention=="Process Intervention by IO"
replace p_pro_IO_int = 0 if p_pro_IO_intervention=="No Intervention by IO"

* Generate nice labels for the regression

label variable pi "Polarization \$ \pi \$"
label variable p_pro_IO_int "Intervention by IO"
label variable year "Year"

reg p pi p_pro_IO_int year if sample==1 & (intervener=="United States" | i_ccode==2), robust
outreg2 using "../tables_production/table_6_1_us_interventions_IO.tex" ,tex(fragment) label replace ctitle(U.S. Process Interventions)

reg c pi p_pro_IO_int year if sample==1 & (intervener=="United States" | i_ccode==2), robust
outreg2 using  "../tables_production/table_6_1_us_interventions_IO.tex" ,tex(fragment) label append ctitle(U.S. Candidate Interventions)

* Reset the labels to normal

label variable pi "Polarization"

////////////////////////////////////////////////////////////////////////////////
/*
Figures on interventions by France and the United Kingdom
*/
////////////////////////////////////////////////////////////////////////////////

* Generate some useful variables

gen franceinter_helper =1 if sample==1 & someinter == 1 & (i_ccode==220) & ((p!=. & p!=0) | (c!=. & c!=0))
bysort electionid: egen franceinter =  max(franceinter_helper) if sample==1
replace franceinter = 0 if franceinter!=1 & someinter == 1

gen ukinter_helper =1 if sample==1 & someinter == 1 & (i_ccode==200) & ((p!=. & p!=0) | (c!=. & c!=0))
bysort electionid: egen ukinter =  max(ukinter_helper) if sample==1
replace ukinter = 0 if ukinter!=1 & someinter == 1

* Generate graphs

graph bar ukinter franceinter if electionid_uni==1 & sample==1, over(period) ytitle("Fraction with Presence of ...") ///
legend(lab(1 "United Kindom") lab(2 "France")) ylab(0 (0.1) 0.5)
graph export  ..\graphs_production_v2\fig_6_5a_france_uk_presence.pdf, replace

/*----------------------------------------------------------------------------*/

graph hbar c_abs_intervener if sample==1 & (i_ccode==200 | i_ccode==220), ///
over(period) by(intervener, note("")) ylab(0(.25) .5) ///
ytitle("Conditional Mean of (Absolute) Candidate Intervention |c|") bar(1, fcolor(white) lcolor(black)) yline(0, lcolor(black*.33))
graph export  ..\graphs_production_v2\fig_6_6c_c_by_period_france_uk.pdf, replace

/*----------------------------------------------------------------------------*/

graph hbar p if sample==1 & (i_ccode==200 | i_ccode==220),  ///
over(period) by(intervener, note("")) ylab(-.5 (.25) .5) ///
ytitle("Conditional Mean of Process Intervention") bar(1, fcolor(white) lcolor(black)) yline(0, lcolor(black*.33))
graph export  ..\graphs_production_v2\fig_6_6a_p_by_period_france_uk.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Figures on interventions by Russia and China
*/
////////////////////////////////////////////////////////////////////////////////

* Generate some useful variables

gen russiainter_helper =1 if sample==1 & someinter == 1 & (i_ccode==365) & ((p!=. & p!=0) | (c!=. & c!=0))
bysort electionid: egen russiainter =  max(russiainter_helper) if sample==1
replace russiainter = 0 if russiainter!=1 & someinter == 1

gen chinainter_helper =1 if sample==1 & someinter == 1 & (i_ccode==710) & ((p!=. & p!=0) | (c!=. & c!=0))
bysort electionid: egen chinainter =  max(chinainter_helper) if sample==1
replace chinainter = 0 if chinainter!=1 & someinter == 1

* Generate graphs

graph bar russiainter chinainter if electionid_uni==1 & sample==1, over(period) ytitle("Fraction with Presence of ...") ///
legend(lab(1 "USSR/Russia") lab(2 "China")) ylab(0 (0.1) 0.5)
graph export  ..\graphs_production_v2\fig_6_5b_russia_china_presence.pdf, replace

/*----------------------------------------------------------------------------*/

graph hbar p if sample==1 & (i_ccode==710 | i_ccode==365), ///
over(period) by(intervener, note("")) ylab(-.5 (.25) .5) ///
ytitle("Conditional Mean of Process Intervention") bar(1, fcolor(white) lcolor(black)) yline(0, lcolor(black*.33))
graph export  ..\graphs_production_v2\fig_6_6b_p_by_period_china_russia.pdf, replace

/*----------------------------------------------------------------------------*/

graph hbar c_abs_intervener if sample==1 & (i_ccode==710 | i_ccode==365), ///
over(period) by(intervener, note("")) ylab(0(.25) .5) ///
ytitle("Conditional Mean of (Absolute) Candidate Intervention |c|") bar(1, fcolor(white) lcolor(black)) yline(0, lcolor(black*.33)) 
graph export  ..\graphs_production_v2\fig_6_6d_c_by_period_china_russia.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Bias in hegemonies: liberal vs. aliberal hegemons
*/
////////////////////////////////////////////////////////////////////////////////

gen hegwar_li_graph = hegwar_li
replace hegwar_li_graph = . if hegwar_li>2
label values hegwar_li_graph  LI_long

bysort pi hegwar_li: egen biasinc_mean_lib = mean(biasinc) if sample==1 & i_power=="H"
bysort hegwar_li: egen biasinc_mean_lib_overall = mean(biasinc) if sample==1 & i_power=="H"

bysort biasinc pi hegwar_li sample: gen howmany=_N if sample==1 & i_power=="H"
egen tag=tag(biasinc pi hegwar_li sample)if sample==1 & i_power=="H"
sort pi
ttest biasinc if sample==1 & i_power=="H", by(hegwar_li_graph) unequal
return list
local tnote = "The p-value of a t-test of the difference of means `: di %4.2f `r(mu_1)'' - `: di %4.2f `r(mu_2)'' is p=`: di %4.3f `r(p)'' "
di "`tnote'"
twoway ///  
(bar biasinc_mean_lib pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.5) fcolor(none))  ///
(scatter biasinc pi [w=howmany] if sample==1 & tag==1 & i_power=="H", by(hegwar_li_graph, note("`tnote'") legend(off)) msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(black*.25))) ///
(scatter biasinc pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
(line biasinc_mean_lib_overall pi if sample==1 & tag==1, lcolor(black) lpattern(solid)) ///
, legend(off) ytitle("Bias Favoring Incumbent")  ylab(-.25 (.25) 1.25) xtitle("Polarization")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_6_7_biasinc_pi_liberalism_hegemon.pdf, replace

////////////////////////////////////////////////////////////////////////////////
/*
Figure on US process interventions in the presence of international organizations
*/
////////////////////////////////////////////////////////////////////////////////

bysort sample pi p_pro_IO_intervention i_ccode: egen p_IO_mean_US_ipower=mean(p) if sample==1 & (intervener=="United States" | i_ccode==2)
bysort sample p_pro_IO_intervention i_ccode: egen  p_IO_mean_US_ipower_overall=mean(p) if sample==1 & (intervener=="United States" | i_ccode==2)

bysort p pi p_pro_IO_intervention i_ccode sample: gen howmany=_N if sample==1 & (intervener=="United States" | i_ccode==2)
egen tag=tag(p pi p_pro_IO_intervention i_ccode sample )if sample==1 & (i_ccode==2)
sort pi
ttest p if sample==1 & (intervener=="United States" | i_ccode==2), by(p_pro_IO_intervention) unequal
local tnote = "The p-value of a t-test of the difference of means `: di %4.3f `r(mu_1)'' - `: di %4.3f `r(mu_2)'' is p=`: di %4.3f `r(p)'' "
twoway ///  
(bar p_IO_mean_US_ipower pi if sample==1 & tag==1, barwidth(.25) lcolor(black*0.66) fcolor(none))  ///
(scatter p pi [w=howmany] if sample==1 & tag==1 & (intervener=="United States" | i_ccode==2), by(p_pro_IO_intervention,  note("`tnote'") legend(off)) msymbol(circle) mcolor(black%10) mlcolor(white%0) msize(large) yline(0, lcolor(black*.33))) ///
(scatter p pi if sample==1 & tag==1, mlabel(howmany) mlabposition(0) msymbol(none) mlabcolor(black) mlabsize(small)) ///
(line p_IO_mean_US_ipower_overall pi if sample==1 & tag==1, lcolor(black) lpattern(solid)) ///
, legend(off) ytitle("Process Intervention")  ylab(-.25 (.25) 1.25) xtitle("Polarization")
cap drop howmany tag
graph export  ..\graphs_production_v2\fig_6_8_us_interventions_IO.pdf, replace
